package service.impl;

import dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import service.UserService;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

//<bean id="userService" class="service.impl.UserServiceImpl">
//@Component("userService")
@Service("userService")
public class UserServiceImpl implements UserService {
//    <property name="userDao" ref="userDao"/>
//    @Autowired //只写Autowired注解，则按照数据类型从Spring容器中匹配UserDao类型的Bean
//    @Qualifier("userDao") //按照bean的id从容器中匹配，必须和@Autowired结合使用
    @Resource(name = "userDao") //推荐使用：相当于@Autowired+@Qualifier，按照名称进行注入
    private UserDao userDao;

    //如果使用的注解配置，则不需要setter方法
    //如果使用xml配置文件进行set注入，则需要配置setter方法
/*    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }*/

    @Override
    public void doSave() {
        userDao.save();
    }

    @PostConstruct //构造器之后
    public void init(){
        System.out.println("Service对象的初始化方法运行...");
    }

    @PreDestroy //销毁之前
    public void destroy(){
        System.out.println("Service对象的销毁方法运行...");
    }
}
